package cn.lena.idle;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.List;

import cn.lena.idle.adapter.MySellAdapter;
import cn.lena.idle.dao.GoodsDbHelper;
import cn.lena.idle.pojo.DTO.CurrentGoods;
import cn.lena.idle.pojo.DTO.LoginUser;
import cn.lena.idle.pojo.DTO.Version;
import cn.lena.idle.pojo.Goods;

/**
 * @author lena
 * @date 2021/6/15
 */
public class MySellActivity extends AppCompatActivity {

    private ImageView back;
    private GoodsDbHelper goods_db;
    private ListView lv;
    private List<Goods> lists;
    private MySellAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_sell);

        back=(ImageView)findViewById(R.id.rb1);
        lv=(ListView)findViewById(R.id.selllv);
        adapter=new MySellAdapter(getApplicationContext());

        // 初始化页面
        initPage();

        // 单击某个商品 进入商品详情页 t
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Goods current = (Goods)adapter.getItem(position);
                toDetail(current);
            }
        });

        // 返回上一页
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MySellActivity.this,MyActivity.class);
                startActivity(intent);
            }
        });

    }

    /**
     * 单击跳转到商品详情页面 t
     */
    public void toDetail(Goods goods){
        CurrentGoods.SELECT=goods;
        Intent intent=new Intent(MySellActivity.this, GoodsDetailActivity2.class);
        startActivity(intent);
    }

    /**
     * 初始化页面
     */
    private void initPage(){
        goods_db=new GoodsDbHelper(getApplicationContext(),GoodsDbHelper.DB_NAME,null, Version.GOODS);
        lists=goods_db.getDelete(LoginUser.CURRENT_USER.getId(),1);
        //System.out.println("===list===>"+lists);
        if(!lists.isEmpty()) {
            adapter.setData(lists);
            lv.setAdapter(adapter);
        }
        else{
            Toast.makeText(this,"还没有卖出的商品",Toast.LENGTH_LONG).show();
        }
    }

}
